package org.sith.gl.taskmanager.client.tasks;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.datepicker.client.DatePicker;
import org.sith.gl.taskmanager.client.UsersRemoteService;
import org.sith.gl.taskmanager.client.beans.UserEntityGWT;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;



public class AddTaskPopup {

    public static final DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("dd/MM/yyyy");

    private AddTaskPopup() {


    }

    public static void show(final UserEntityGWT addedBy) {
        final PopupPanel popupPanel = new PopupPanel();
        popupPanel.setTitle("Add task");

        popupPanel.setPopupPosition(300, 100);
        VerticalPanel main = new VerticalPanel();

        popupPanel.add(main);

        main.add(createLine("Added By: ", new Label(addedBy.getLogin())));

        final List<UserEntityGWT> users = new ArrayList<UserEntityGWT>();
        final ListBox usersList = initUsersList(users);
        main.add(createLine("Assigned To", usersList));


        final DatePicker creationDatePicker = new DatePicker();
        creationDatePicker.setValue(new Date(), true);
        main.add(createLine("Creation date: ", createDateButton(creationDatePicker)));

        final DatePicker dueDatePicker = new DatePicker();
        dueDatePicker.setValue(new Date(), true);
        main.add(createLine("Due date: ", createDateButton(dueDatePicker)));

        final TextBox desc = new TextBox();
        main.add(createLine("Task description: ", desc));


        Button saveButton = new Button("Save");
        saveButton.addClickHandler(new SaveButtonClickHandler(new FormWidgets(addedBy, usersList, users, desc, creationDatePicker, dueDatePicker, popupPanel)));
        main.add(saveButton);
        popupPanel.show();

    }

    private static Button createDateButton(final DatePicker dueDatePicker) {
        final Button dateButton = new Button("Choose Date");
        dateButton.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent clickEvent) {
                final PopupPanel datePopup = new PopupPanel();
                datePopup.setPopupPosition(200, 200);
                dueDatePicker.addValueChangeHandler(new ValueChangeHandler() {

                    @Override
                    public void onValueChange(ValueChangeEvent valueChangeEvent) {
                        Date date = (Date) valueChangeEvent.getValue();
                        dateButton.setText(dateTimeFormat.format(date));
                        datePopup.hide();
                    }
                });
                dueDatePicker.setValue(new Date(), true);
                datePopup.add(dueDatePicker);
                datePopup.show();

            }
        });
        return dateButton;
    }

    private static ListBox initUsersList(final List<UserEntityGWT> users) {
        final ListBox usersList = new ListBox();

        UsersRemoteService.App.getInstance().getAllUsers(new AsyncCallback<List<UserEntityGWT>>() {


            @Override
            public void onFailure(Throwable throwable) {
                Window.alert("Error by receiving users");
            }

            @Override
            public void onSuccess(List<UserEntityGWT> userEntityGWTs) {
                users.addAll(userEntityGWTs);

                usersList.addItem("Choose user", "-1");
                for (UserEntityGWT user : users) {
                    usersList.addItem(user.getLogin());
                }
            }
        });
        return usersList;
    }

    private static HorizontalPanel createLine(String name, Widget field) {
        HorizontalPanel line = new HorizontalPanel();
        Label label = new Label(name);
        line.add(label);
        line.add(field);
        line.setCellHorizontalAlignment(label, HasHorizontalAlignment.ALIGN_RIGHT);
        line.setCellHorizontalAlignment(field, HasHorizontalAlignment.ALIGN_LEFT);
        return line;
    }

}
